RestTemplate এর মাধ্যমে HTTP GET, POST, PUT, DELETE রিকোয়েস্ট করা

Java Technologies - স্প্রিং বুট ক্লায়েন্ট (Spring Boot Client) - RestTemplate এর পরিচিতি
153

স্প্রিং বুটে RestTemplate ব্যবহার করে HTTP GET, POST, PUT, DELETE রিকোয়েস্ট করার জন্য নিচের ধাপগুলো অনুসরণ করতে পারেন:


১. RestTemplate কনফিগারেশন:

প্রথমে একটি RestTemplate বীন কনফিগার করুন:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class AppConfig {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

২. HTTP GET রিকোয়েস্ট:

GET রিকোয়েস্টের মাধ্যমে ডেটা রিট্রিভ করতে getForObject বা getForEntity ব্যবহার করা হয়।

উদাহরণ:

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ApiService {
    private final RestTemplate restTemplate;

    public ApiService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String getApiResponse(String url) {
        // GET Request
        return restTemplate.getForObject(url, String.class);
    }
}

কল করা:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Client {
    @Autowired
    private ApiService apiService;

    public void getRequestExample() {
        String response = apiService.getApiResponse("http://example.com/api/data");
        System.out.println(response);
    }
}

৩. HTTP POST রিকোয়েস্ট:

POST রিকোয়েস্টের মাধ্যমে ডেটা পাঠাতে postForObject বা postForEntity ব্যবহার করা হয়।

উদাহরণ:

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ApiService {
    private final RestTemplate restTemplate;

    public ApiService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String postApiResponse(String url, Object requestObject) {
        // POST Request
        ResponseEntity<String> response = restTemplate.postForEntity(url, requestObject, String.class);
        return response.getBody();
    }
}

কল করা:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Client {
    @Autowired
    private ApiService apiService;

    public void postRequestExample() {
        String url = "http://example.com/api/create";
        MyRequest requestObject = new MyRequest("value1", "value2");
        String response = apiService.postApiResponse(url, requestObject);
        System.out.println(response);
    }
}

class MyRequest {
    private String field1;
    private String field2;

    // Constructors, Getters, Setters
    public MyRequest(String field1, String field2) {
        this.field1 = field1;
        this.field2 = field2;
    }

    // Getters and Setters...
}

৪. HTTP PUT রিকোয়েস্ট:

PUT রিকোয়েস্টের মাধ্যমে ডেটা আপডেট করতে put ব্যবহার করা হয়।

উদাহরণ:

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ApiService {
    private final RestTemplate restTemplate;

    public ApiService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public void updateResource(String url, Object requestObject) {
        // PUT Request
        restTemplate.put(url, requestObject);
    }
}

কল করা:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Client {
    @Autowired
    private ApiService apiService;

    public void putRequestExample() {
        String url = "http://example.com/api/update/1";
        MyRequest requestObject = new MyRequest("updatedValue1", "updatedValue2");
        apiService.updateResource(url, requestObject);
        System.out.println("Resource updated successfully.");
    }
}

৫. HTTP DELETE রিকোয়েস্ট:

DELETE রিকোয়েস্টের মাধ্যমে ডেটা মুছতে delete ব্যবহার করা হয়।

উদাহরণ:

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ApiService {
    private final RestTemplate restTemplate;

    public ApiService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public void deleteResource(String url) {
        // DELETE Request
        restTemplate.delete(url);
    }
}

কল করা:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Client {
    @Autowired
    private ApiService apiService;

    public void deleteRequestExample() {
        String url = "http://example.com/api/delete/1";
        apiService.deleteResource(url);
        System.out.println("Resource deleted successfully.");
    }
}

সংক্ষেপে:

HTTP মেথডRestTemplate মেথড
GETgetForObject, getForEntity
POSTpostForObject, postForEntity
PUTput
DELETEdelete

নোট:

  1. Error Handling: RestTemplate এর সাথে Exception Handling এর জন্য try-catch ব্লক বা Spring এর RestClientException ব্যবহার করতে পারেন।
  2. Headers এবং Authentication: HttpHeaders এবং HttpEntity ব্যবহার করে রিকোয়েস্টের সাথে হেডার যোগ করা সম্ভব।

এইভাবে আপনি RestTemplate ব্যবহার করে বিভিন্ন HTTP মেথডের রিকোয়েস্ট তৈরি এবং পরিচালনা করতে পারবেন।

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...